#!/usr/bin/env python
# encoding: utf-8
"""
@author:shucheng
@file:constant.py
@time:2024/1/4 22:37
@desc 常量
"""

from pr_properties import pr_properties

from util import file_tool

CONFIG_PROPERTIES_FILE_RELATIVE_PATH = 'resources/config.properties'

# 金融数据本地存储根路径
FINANCE_DATA_ROOT_PATH = "finance.data.root_path"


class ConfigFile:
    def __init__(self):
        config_file_path = file_tool.get_project_file(CONFIG_PROPERTIES_FILE_RELATIVE_PATH)
        p = pr_properties.read(config_file_path)
        # 存储配置信息的handler
        self._p = p

    def get(self, key):
        """
        根据key获取配置的值
        :param key:
        :return:
        """
        return self._p.get(key)

    def get_starts_with(self, prefix, keep_prefix=True) -> {}:
        """
        获取key以特定前缀开头的字典项
        :param prefix: 前缀
        :return:

        e.g. config.get_starts_with('finance.data.database')
        """
        properties = self._p.properties
        result = {}
        for k, v in properties.items():
            if not k.startswith(prefix):
                continue

            if keep_prefix:
                result[k] = v
            else:
                new_k = k[len(prefix) + 1:]
                result[new_k] = v
        return result


config = ConfigFile()
